/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.param.service.impl;

import java.sql.Connection;
import java.util.List;

import net.comm.common.ReturnMessage;

import net.comm.param.bean.BaseCnpca;
import net.comm.param.db.DbCnpcaService;
import net.comm.param.db.CnpcaServiceSqlMysql;
import net.comm.param.web.form.CnpcaForm;
import net.comm.page.PageCtr;

import net.comm.util.DbcpPool;

import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;

/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class CnpcaServiceImpl {

	static Logger logger = Logger.getLogger(CnpcaServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(CnpcaForm theForm,  ReturnMessage msg) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			CnpcaServiceSqlMysql servSql = new CnpcaServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql1();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, null));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[2];
			
			fields2[1] = page.getPos()-1;
			fields2[2] = page.getPageSize();
			
			sql = servSql.getRecbyPageSql();

			list = bSqlServ.dbRead(cn, sql, fields2, new DbCnpcaService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}

	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public int  getByName(String name) 
	{
		int id = 0;
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			fields[0] = name;
			
			CnpcaServiceSqlMysql servSql = new CnpcaServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getRecbyNameSql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields, new DbCnpcaService());
			
			if(list.size() > 0){
				BaseCnpca base = (BaseCnpca)list.get(0);			
				id = base.getId();
			}
			
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about cnpca" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		return id;
	}
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public String  getById(int id) 
	{
		String name = "";
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			fields[0] = id;
			
			CnpcaServiceSqlMysql servSql = new CnpcaServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields, new DbCnpcaService());
			
			if(list.size() > 0){
				BaseCnpca base = (BaseCnpca)list.get(0);			
				name = base.getName();
			}
			
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about cnpca" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		return name;
	}
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public List getbyPid(int pid) 
	{
		List list = null;
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			fields[0] = pid;
			
			CnpcaServiceSqlMysql servSql = new CnpcaServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getRecbyPidSql();			
					
			list = bSqlServ.dbRead(cn, sql, fields, new DbCnpcaService());
			
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about cnpca" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public List getbyCid(int cid) 
	{
		List list = null;
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			fields[0] = cid;
			
			CnpcaServiceSqlMysql servSql = new CnpcaServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getRecbyCidSql();			
					
			list = bSqlServ.dbRead(cn, sql, fields, new DbCnpcaService());
			
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about cnpca" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}
	/**
	 * 添加
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(CnpcaForm theForm, ReturnMessage msg) 
	{
		
		return ;
	}

	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(CnpcaForm theForm, ReturnMessage msg) 
	{
		
		return ;
	}

	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(CnpcaForm theForm, ReturnMessage msg) 
	{
		return ;
	}

}
